﻿using System;
using EmbeddedSharp.Core;
using System.IO;

namespace EmbeddedSharp.Scripting.Function.Context {

    /// <summary>
    /// Run a external script file
    /// </summary>
    [Function("run", "run(fileName)", "run(\"hello.ess\");", "Hello, world!")]
    public class RunNode : ContextFuncNode {

        /// <summary>
        /// Default constructor
        /// </summary>
        public RunNode()
            : base() {
            base.Value = "run";
        }

        /// <summary>
        /// Evaluate the expression
        /// </summary>
        /// <returns></returns>
        public override object Evaluate() {
            if (Parameters.Count < 0) {
                throw new ArgumentException(
                    "usage: run(program-file-name)\nexample:run(\"program.ess\")");
            }

            string fileName = 
                Convert.ToString(this.Parameters[0].Evaluate());
            if(!File.Exists(fileName)) {
                throw new FileNotFoundException(fileName);
            }

            base.Context.Run(fileName);
            return string.Empty;
        }
    }
}
